package cn.gengar.infrastructure.adapter.repository;

import cn.gengar.domain.activity.adapter.repository.IActivityRepository;
import cn.gengar.domain.activity.model.valobj.DiscountTypeEnum;
import cn.gengar.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.gengar.domain.activity.model.valobj.SCSkuActivityVO;
import cn.gengar.domain.activity.model.valobj.SkuVO;
import cn.gengar.infrastructure.dao.IGroupBuyActivityDao;
import cn.gengar.infrastructure.dao.IGroupBuyDiscountDao;
import cn.gengar.infrastructure.dao.ISCSkuActivityDao;
import cn.gengar.infrastructure.dao.ISkoDao;
import cn.gengar.infrastructure.dao.po.GroupBuyActivity;
import cn.gengar.infrastructure.dao.po.GroupBuyDiscount;
import cn.gengar.infrastructure.dao.po.SCSkuActivity;
import cn.gengar.infrastructure.dao.po.Sku;
import cn.gengar.infrastructure.dcc.DCCService;
import cn.gengar.infrastructure.redis.RedissonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RBitSet;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Objects;

@Repository
@Slf4j
public class ActivityRepository implements IActivityRepository {

    @Resource
    private IGroupBuyActivityDao groupBuyActivityDao;
    @Resource
    private IGroupBuyDiscountDao groupBuyDiscountDao;
    @Resource
    private ISkoDao skuDao;
    @Resource
    private ISCSkuActivityDao skuActivityDao;
    @Resource
    private RedissonService redissonService;
    @Resource
    private DCCService dccService;


    @Override
    public GroupBuyActivityDiscountVO queryGroupBuyActivityDiscountVO(Long activityId) {
        GroupBuyActivity groupBuyActivityRes = groupBuyActivityDao.queryValidGroupBuyActivityId(activityId);
        if (Objects.isNull(groupBuyActivityRes) || Objects.isNull(groupBuyActivityRes.getDiscountId())) {
            log.warn("groupBuyActivityRes is null");
            return null;
        }

        String discountId = groupBuyActivityRes.getDiscountId();
        GroupBuyDiscount groupBuyDiscountRes = groupBuyDiscountDao.queryGroupBuyActivityDiscountByDiscountId(discountId);
        if (Objects.isNull(groupBuyDiscountRes)) {
            return null;
        }

        GroupBuyActivityDiscountVO resVO = new GroupBuyActivityDiscountVO();

        GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount = GroupBuyActivityDiscountVO.GroupBuyDiscount.builder()
                .discountName(groupBuyDiscountRes.getDiscountName())
                .discountDesc(groupBuyDiscountRes.getDiscountDesc())
                .discountType(DiscountTypeEnum.getByCode(groupBuyDiscountRes.getDiscountType()))
                .marketPlan(groupBuyDiscountRes.getMarketPlan())
                .marketExpr(groupBuyDiscountRes.getMarketExpr())
                .tagId(groupBuyDiscountRes.getTagId())
                .build();

        // 活动信息
        BeanUtils.copyProperties(groupBuyActivityRes, resVO);

        // 优惠信息
        resVO.setGroupBuyDiscount(groupBuyDiscount);

        return resVO;
    }

    @Override
    public SkuVO querySkuVO(String goodsId) {
        Sku sku = skuDao.querySkuByGoodsId(goodsId);
        if (Objects.nonNull(sku)) {
            SkuVO skuVO = new SkuVO();
            BeanUtils.copyProperties(sku, skuVO);
            return skuVO;
        }
        return null;
    }

    @Override
    public SCSkuActivityVO querySCSkuActivityBySCGoodsId(String source, String channel, String goodsId) {
        SCSkuActivity scSkuActivityReq = SCSkuActivity.builder()
                .source(source)
                .channel(channel)
                .goodsId(goodsId)
                .build();

        SCSkuActivity scSkuActivity = skuActivityDao.querySCSkuActivityBySCGoodsId(scSkuActivityReq);
        if (Objects.isNull(scSkuActivity))  {
            return null;
        }

        return SCSkuActivityVO.builder()
                .source(scSkuActivity.getSource())
                .chanel(scSkuActivity.getChannel())
                .activityId(scSkuActivity.getActivityId())
                .goodsId(scSkuActivity.getGoodsId())
                .build();
    }

    @Override
    public boolean isTagCrowdRange(String tagId, String userId) {
        if (StringUtils.isAllBlank(tagId, userId)) {
            return true;
        }

        RBitSet bitSet = redissonService.getBitSet(tagId);
        if (!bitSet.isExists()) {
            // 不存在，直接设置为默认在标签中
            return true;
        }
        // 判断用户是否存在人群中
        return bitSet.get(redissonService.getIndexFromUserId(userId));
    }

    @Override
    public boolean downgradeSwitch() {
        return dccService.isDowngradeSwitch();
    }

    @Override
    public boolean cutRange(String userId) {
        return dccService.isCutRange(userId);
    }
}


